<?php

namespace app\command;

use app\admin\model\Admin;
use app\admin\model\Area;
use app\admin\model\hotel\Hotel;
use app\admin\model\Member;
use app\admin\model\vip\Code;
use app\api\model\MemberVipCard;
use app\api\model\Scenic;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Data extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('data');
        // 设置参数
        $this->setDescription('数据统计')->setHelp("php think data");
    }

    protected function execute(Input $input, Output $output)
    {


        $scenic = \think\facade\Db::connect('database_xindong')
            ->name('scenic')
            ->order('id desc')
            ->find();
        $notes = $scenic['book_notice'];



        die;





        $scenic = (new Hotel())->order('id asc')->select();
        foreach ($scenic as $val){

            $area = (new Area())->where('name','like',"%".$val['city_name']."%")->find();
            if(!isset($area['name'])) continue;
            (new Hotel())->where(['id' => $val['id']])->update(['city_name'=> $area['name']]);

            echo $val['id']."\n";
        }


//        //用户卡密
//        $vipCad = \think\facade\Db::connect('database_lvyou')
//            ->name('ty_card')
//            ->where(['switch' => 1])
//            ->select();
//
//
//        foreach ($vipCad as $val){
//            //查询卡密
//            $code = (new Code())->where(['id' => $val['convert_id']])->find();
//
//
//            $data = [
//               'admin_id' => $val['admin_id'],
//               'member_id' => $val['user_id'],
//               'vip_code' => $code['number'] ?? '',
//                'vip_card_id' => 3,
//                'vip_type' => 1,
//                'nums' => null,
//                'expire_time' => $val['overtime'],
//                'status' => 1,
//                'create_time' => $val['createtime'],
//                'update_time' => $val['updatetime'],
//            ];
//
//            (new MemberVipCard())->insert($data);
//            echo $val['id']."\n";
//        }
//
//
//        die;

        //卡密

       $map[] = ['over_time' ,'>' ,time()];
       $map[] = ['status' ,'!=' ,2];

        $code = \think\facade\Db::connect('database_lvyou')->name('ty_convert')
            ->where($map)
            ->count();
       print_r($code);die;

        foreach ($code as $val){
           if($val['status'] == 0 && $val['over_time'] < time()) continue;
            $data = [
                'id' =>$val['id'],
               'admin_id' => $val['admin_id'],
                'vip_card_id' => 3,
                'number' => $val['name'],
                'password' => $val['pass'],
                'status' => $val['status'],
                'user_expire_time' => $val['over_time'],
                'create_time' => $val['createtime'],
            ];
            (new Code())->insert($data);
            echo $val['id']."\n";
        }


//        //管理员
//        $admin = \think\facade\Db::connect('database_lvyou')
//            ->name('admin')
//            ->where('id','>',2)
//            ->select();
//
//        $model = new Admin();
//        foreach ($admin as $val){
//            $data = [
//               'id' => $val['id'],
//               'username' => $val['username'],
//               'nickname' => $val['nickname'],
//               'password' => '813b91946df3fb06bed5c35d73651880',
//                'salt' => 'Opq81lZSkYsTy9Qg',
//                'create_time' => $val['createtime']
//            ];
//            $model->insert($data);
//            \think\facade\Db::name('admin_group_access')->insert(['uid' => $val['id'],'group_id'=>5]);
//            echo $val['id']."\n";
//        }
//
//        die;
        //用户
        $list = \think\facade\Db::connect('database_lvyou')
            ->name('user')
            ->order('id asc')
            ->select();

        $model = new Member();
        foreach ($list as $val){
            if($val['mobile'] == 'NoLoginData' || !$val['mobile']) continue;
            $data = [
               'id' => $val['id'],
               'nickname' => $val['nickname'],
               'avatar' => $val['avatar'],
               'phone' => $val['mobile'],
               'username' => $val['username'],
               'id_number' => $val['bio'],
               'create_time' => $val['createtime'],
            ];

            //查询openid
            $openid = \think\facade\Db::connect('database_lvyou')->name('third')
                ->where(['user_id' => $val['id']])->value('openid');
            if($openid){
                $data['openid'] = $openid;
            }

            $model->insert($data);
            echo $val['id']."\n";
        }


    }

}
